﻿using DevExpress.XtraEditors;
using MDK.Common;
using MDK.Model.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform
{
    public partial class frmResetPassword : BaseForm
    {
        private T_User _user { get; set; }
        public frmResetPassword()
        {
            InitializeComponent();
        }
        public frmResetPassword(string _id) : this()
        {
            try
            {
                PropertyHelper jsonWhere = new PropertyHelper();
                jsonWhere.Addproperty(CompareType.Equal, "Id", _id);
                string url = string.Format("User/Find?jsonWhere={0}", JsonConvert.SerializeObject(jsonWhere.propertyList));
                _user = WebAPIHelper.Get<T_User>(url);
                if (_user == null)
                {
                    FastMessageBox.Error("无法获取用户信息！");
                    Close();
                    Dispose();
                    DialogResult = DialogResult.Abort;
                }
            }
            catch (Exception ex)
            {
                FastMessageBox.Error(ex.Message);
                Close();
                Dispose();
                DialogResult = DialogResult.Abort;
            }
        }

        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(text_NewPassword.Text.Trim())
                && string.IsNullOrEmpty(text_EnsurePassword.Text.Trim()))
            {
                FastMessageBox.Error("密码不能为空!");
            }
            else
            {
                if (text_NewPassword.Text.Trim() == text_EnsurePassword.Text.Trim())
                {
                    _user.Password = DESEncrypt.Encrypt(text_NewPassword.Text.Trim());
                    WebAPIHelper.Post<T_User>("User/Modify", _user);
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    FastMessageBox.Error("两次密码输入不一致!");
                }
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
            DialogResult = DialogResult.Cancel;
        }
    }
}
